package com.carlinfo.stu_ques.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import com.carlinfo.stu_ques.common.util.DateUtil;
import com.carlinfo.stu_ques.common.util.DbUtil;
import com.carlinfo.stu_ques.common.util.FileUtil;
import com.carlinfo.stu_ques.demo.pojo.Dynasty;
import com.carlinfo.stu_ques.demo.pojo.King;
import com.carlinfo.stu_ques.demo.service.IDynastyService;
import com.carlinfo.stu_ques.demo.service.impl.DynastyServiceImpl;
import com.carlinfo.stu_ques.question.pojo.Answer;
import com.carlinfo.stu_ques.question.pojo.Question;
import com.carlinfo.stu_ques.question.service.IQueAnsService;
import com.carlinfo.stu_ques.question.service.impl.QueAnsServiceImpl;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.IUsersService;
import com.carlinfo.stu_ques.users.service.impl.UsersServiceImpl;

public class ClientMain
{

	private UsersClient client ;

	private IUsersService usersService ;
	
	DbUtil dbUtil = new DbUtil();

	/**
	 * 时间的工具类
	 */
	private DateUtil dateUtil = new DateUtil();

	private IDynastyService dynastyServie;

	private IQueAnsService qas ;

	private Map<String, Object> map = new HashMap<String, Object>();

	public static void main(String[] args)
	{
		UsersClient client = new UsersClient();
		IUsersService usersService = new UsersServiceImpl();
		IQueAnsService qas = new QueAnsServiceImpl();
		IDynastyService dynastyService = new DynastyServiceImpl();
		if(qas instanceof QueAnsServiceImpl) {
			QueAnsServiceImpl questionService = (QueAnsServiceImpl) qas;
			questionService.setUsersService(usersService);
		}
		client.setUsersService(usersService);
	
		ClientMain clientMain = new ClientMain();
		clientMain.setDynastyServie(dynastyService);
		clientMain.setUsersService(usersService);
		clientMain.setClient(client);
		clientMain.setQas(qas);
		clientMain.launch();
	}

	private void launch()
	{
		System.out.println("欢迎使用,按下h进入帮助菜单");
		BufferedReader bfr = null;
		try
		{
			InputStream is = System.in;
			InputStreamReader isr = new InputStreamReader(is);
			bfr = new BufferedReader(isr);
			String line = "";
			while (true)
			{
				System.out.println("==主菜单如下==按下n退出,按下h进入帮助菜单");
				line = bfr.readLine();
				if ("n".equalsIgnoreCase(line.trim()))
				{
					System.out.println("你退出了系统！");
					break;
				} else if ("h".equalsIgnoreCase(line.trim()))
				{
					help();
				} else if ("1".equalsIgnoreCase(line.trim()))
				{
					this.dynastyLaunch(bfr);
				} else if ("2".equalsIgnoreCase(line.trim()))
				{
					this.kingLaunch(bfr);
				} else if ("3".equalsIgnoreCase(line.trim()))
				{
					client.userLaunch(bfr);
				} else if ("4".equalsIgnoreCase(line.trim()))
				{
					client.userLaunch2(bfr);
				} else if ("5".equalsIgnoreCase(line.trim()))
				{
					selectListQuestionService();
				} else if ("6".equalsIgnoreCase(line.trim()))
				{
					saveOneQuestionService(bfr);
				} else if ("7".equalsIgnoreCase(line.trim()))
				{
					updateOneQuestionService(bfr);
				} else if ("8".equalsIgnoreCase(line.trim()))
				{
					deleteOneQuestionService(bfr);
				} else if ("9".equalsIgnoreCase(line.trim()))
				{
					selectOneQuestionService(bfr);
				} else if ("10".equalsIgnoreCase(line.trim()))
				{
					selectListAnswerService();
				} else if ("11".equalsIgnoreCase(line.trim()))
				{
					saveOneAnswerService(bfr);
				} else if ("12".equalsIgnoreCase(line.trim()))
				{
					updateOneAnswerService(bfr);
				} else if ("13".equalsIgnoreCase(line.trim()))
				{
					deleteOneAnswerService(bfr);
				} else if ("14".equalsIgnoreCase(line.trim()))
				{
					selectOneAnswerService(bfr);
				} else if ("50".equalsIgnoreCase(line.trim()))
				{
					randomAsk(bfr);
				} else if ("51".equalsIgnoreCase(line.trim()))
				{
					changeToDb();
				} else if ("52".equalsIgnoreCase(line.trim()))
				{
					dbToFile(bfr);
				}else
				{
					System.out.println("请输入已有的编号！");
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (bfr != null)
				{
					bfr.close();
					bfr = null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * 将文件导入数据库
	 */
	private void changeToDb()
	{
		if(this.dynastyServie.dynastyFileToDb() && this.dynastyServie.kingFileToDb()
				&& this.usersService.studentFileToDb() && this.usersService.levelFileToDb()
				&& this.qas.answerFileToDb() && this.qas.questionFileToDb()
				)
		{
			System.out.println("转换成功!");
		} else
		{
			System.out.println("失败了");
		}
	}
	
	/**
	 * 将数据库中的数据导入文件
	 * @param bfr
	 */
	public void dbToFile(BufferedReader bfr)
	{
		try
		{
			System.out.println("请输入你要放的路径:");
			String path = bfr.readLine();
			if(this.dynastyServie.dynastyDbTofile(path) && this.dynastyServie.kingDbTofile(path)
					&& this.usersService.studentDbTofile(path) && this.usersService.levelDbTofile(path)
					&& this.qas.answerDbTofile(path) && this.qas.questionDbTofile(path)
					)
			{
				System.out.println("转换成功!");
			} else
			{
				System.out.println("失败了");
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 随机提问学生
	 * 现根据id查询事先存储的问题
	 * 再随机抽学生回答问题
	 * 学生答对的时候，退出循环
	 * @param bfr
	 */
	private void randomAsk(BufferedReader bfr)
	{
		try
		{
			/**
			 * 先查询一个问题，问题只要不为空，就开始随机抽学生回答问题
			 */
			Question question = this.selectOneQuestionService(bfr);
			Students students = null;
			if (question != null)
			{
				while (true)
				{
					Random random = new Random();
					int count = 1;
					while (true)
					{
						int tempId = random.nextInt(this.usersService.selectMaxIdService())+1;
						Map<String, Object> condMap = new HashMap<String, Object>();
						condMap.put("id",tempId);
						students = this.usersService.selectOneStudentService(condMap);
						System.out.println(students);
						if (this.usersService.askIsStudentService(students))
						{
							System.out.println(this.usersService.selectMaxIdService() + "==恭喜你，中奖了!==抽的次数:" + count);
							System.out.println(this.usersService.printTitleStu());
							System.out.println(students);
							break;
						}
						count++;
					}

					System.out.println("请输入你的答案:答案|(1)正确或者(0)错误");
					System.out.println("示例:2|0");
					String line = bfr.readLine();
					line = line.trim();
					String[] lines = line.split("\\|");
					if (lines.length != 2)
					{
						System.out.println("你输入的格式有误");
						return;
					}
					Answer answer = new Answer();
					answer.setJieGuo(lines[0]);
					answer.setShiFou("1".equalsIgnoreCase(lines[1]) ? true : false);
					answer.setCreateTime(new Date());
					answer.setEndTime(new Date());
					answer.setQuestionId(question.getId());
					answer.setStuId(students.getId());
					this.qas.saveOneAnswerService(answer);
					if (Integer.valueOf(lines[1]) == 1)
					{
						System.out.println("你终于答对了");
						break;
					}
				}
			}
		} catch (NumberFormatException e)
		{
			System.out.println("输入的ID格式不正确");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/* 查询一个皇上 */
	private King selectOneKingService(BufferedReader bfr)
	{
		System.out.println("请输入你要查询的id");
		try
		{
			String line = bfr.readLine();
			map.put("id", Integer.valueOf(line.trim()));
			King king = this.dynastyServie.selectOneKingService(map);
			if (king != null)
			{
				System.out.println(dynastyServie.kingTitle());
				System.out.println(king);
			} else
			{
				System.out.println("无此纪录，查询失败！");
			}
			return king;
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.selectOneKingService(bfr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/* 删除一个皇上 */
	private void deleteOneKingService(BufferedReader bfr)
	{
		System.out.println("请输入你要删除的皇上id");
		try
		{
			String line = bfr.readLine();
			map.put("id", Integer.valueOf(line.trim()));
			King king = this.dynastyServie.selectOneKingService(map);

			if (king != null)
			{

				this.dynastyServie.deleteOneKingService(king);
				System.out.println("===============删除之后=================");
				this.selectListKingService();
			} else
			{
				System.out.println("无此记录，刪除失败");
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.deleteOneKingService(bfr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/* 更新一个皇上 */
	private void updateOneKingService(BufferedReader bfr)
	{

		King king = this.selectOneKingService(bfr);
		try
		{

			if (king != null)
			{
				System.out.println("请依次输入朝代的相关信息:(朝代id|名字|年龄|身高|庙号|年号)");
				System.out.println("示例:" + king.getDynastyId() + "|" + king.getName() + "|" + king.getAge() + "|"
						+ king.getHeight() + "|" + king.getMiaoHao() + "|" + king.getNianHao());

				String line = bfr.readLine();
				line = line.trim();
				String[] lines = line.split("\\|");
				if (lines.length != 6)
				{
					System.out.println("你输入的格式有误，请重新输入");
					return;
				}
				king.setHeight(Double.valueOf(lines[3]));
				king.setName(lines[1]);
				king.setMiaoHao(lines[4]);
				king.setNianHao(lines[5]);
				king.setAge(Short.valueOf(lines[2]));
				king.setDynastyId(Integer.valueOf(lines[0]));
				this.dynastyServie.updateOneKingService(king);
				System.out.println("===============更新之后=================");
				this.selectListKingService();
			} else
			{
				System.out.println("无此记录，更新失败");
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.updateOneKingService(bfr);
		} catch (IOException e)
		{

			e.printStackTrace();
		}
	}

	/* 添加一个皇上 */
	private void saveOneKingTest(BufferedReader bfr)
	{
		King king = new King();
		System.out.println("请依次输入朝代的相关信息:(朝代id|名字|年龄|身高|庙号|年号)");
		System.out.println("示例:2|李渊|56|165|开元|开元");
		/**
		 * 为一个朝代赋值
		 */
		/* id,朝代Id,名字,年龄,身高,庙号,年号,创建时间,更新时间 */
		try
		{
			String line = bfr.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 6)
			{
				System.out.println("你输入的信息有误,请你重新输入");
				return;
			}

			king.setDynastyId(Integer.valueOf(lines[0]));
			king.setMiaoHao(lines[4]);
			king.setHeight(Short.valueOf(lines[3]));
			king.setNianHao(lines[5]);
			king.setAge(Short.valueOf(lines[2]));
			king.setCreateTime(new Date());
			king.setUpdateTime(new Date());
			king.setName(lines[1]);
			king.setUpdateTime(new Date());

			int res = this.dynastyServie.saveOneKingService(king);

			System.out.println("==添加成功==" + res);
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.saveOneKingTest(bfr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/* 查询一个朝代 */
	private Dynasty selectOneService(BufferedReader bfr)
	{
		try
		{
			System.out.println("请输入你要查询的朝代id");
			String line = bfr.readLine();
			map.put("id", Integer.valueOf(line.trim()));
			Dynasty dynasty = dynastyServie.selectOneDynastyService(map);
			if (dynasty != null)
			{
				System.out.println(dynastyServie.title());
				System.out.println(dynasty);
				return dynasty;
			} else
			{
				System.out.println("查询不到此条记录！");
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.selectOneService(bfr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除一个
	 * 
	 * @param bfr
	 */
	private void deleteOneDynastyService(BufferedReader bfr)
	{
		try
		{
			System.out.println("请输入你要删除的朝代ID");
			String line = bfr.readLine();
			map.put("id", Integer.valueOf(line.trim()));
			Dynasty dynasty = this.dynastyServie.selectOneDynastyService(map);
			/**
			 * 删除之前先判断是否存在此记录，防止空指针
			 */
			if (dynasty != null)
			{
				System.out.println(dynasty);
				dynastyServie.deleteOneDynastyService(dynasty);
				System.out.println("==========删除之后==========");
				this.selectListService();
			} else
			{
				System.out.println("无此记录，删除失败！");
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.deleteOneDynastyService(bfr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 更新一个
	 * 
	 * @param bfr
	 */
	private void updateOneDynastyService(BufferedReader bfr)
	{

		Dynasty dynasty = this.selectOneService(bfr);
		try
		{
			if (dynasty != null)
			{
				System.out.println("请依次输入朝代的相关信息:(名字|年龄|首都|开国时间)");
				String line = "示例:" + dynasty.getName() + "|" + dynasty.getAge() + "|" + dynasty.getCapital() + "|"
						+ dateUtil.dateToStr(dynasty.getSt());
				System.out.println(line);
				line = bfr.readLine();
				String[] lines = line.split("\\|");
				if (lines.length != 4)
				{
					System.out.println("你输入的格式不对");
					return;
				}
				dynastyServie.updateOneDynastyService(dynasty);
				dynasty.setAge(Short.valueOf(lines[1]));
				dynasty.setCapital(lines[2]);
				dynasty.setName(lines[0]);
				dynasty.setUpdateTime(new Date());
				dynasty.setSt(dateUtil.strToDate(lines[3]));
				System.out.println("==========更新之后==========");
				/* 更新之后，再查一遍 */
				this.selectListService();
			} else
			{
				System.out.println("无此记录，更新失败！");
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数，请重新输入");
			this.updateOneDynastyService(bfr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 添加一个
	 * 
	 * @param bf
	 */
	private void saveOneServiceTest(BufferedReader bf)
	{
		try
		{
			System.out.println("请依次输入朝代的相关信息:(名字|年龄|首都|开国时间)");
			System.out.println("示例:明朝|279|北京|1368-01-01");
			String line = bf.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");

			if (lines.length != 4)
			{
				System.out.println("你输入的格式有误，请重新输入");
				return;
			}

			Dynasty dynasty = new Dynasty();
			/**
			 * 为一个朝代赋值
			 */
			dynasty.setAge(Short.valueOf(lines[1]));
			dynasty.setCapital(lines[2]);
			dynasty.setCreateTime(new Date());
			dynasty.setName(lines[0]);
			dynasty.setUpdateTime(new Date());
			dynasty.setSt(dateUtil.strToDate(lines[3]));
			int res = dynastyServie.saveOneDynastyService(dynasty);
			System.out.println("==成功添加==" + res);
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数，请重新输入");
			this.saveOneServiceTest(bf);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 查询所有
	 */
	private void selectListService()
	{
		List<Dynasty> list = dynastyServie.selectListDynastyService();
		System.out.println(dynastyServie.title());
		for (Dynasty dynasty : list)
		{
			System.out.println(dynasty);
		}
	}

	/**
	 * 朝代的菜单
	 * 
	 * @param br
	 */
	private void dynastyLaunch(BufferedReader br)
	{
		String line = "";
		try
		{
			while (true)
			{
				System.out.println("==朝代操作==按下n退出,按下h进入帮助菜单");
				line = br.readLine();
				if ("n".equalsIgnoreCase(line.trim()))
				{
					System.out.println("你退出了朝代操作,你已进入主菜单！");
					break;
				} else if ("h".equalsIgnoreCase(line.trim()))
				{
					dynastyHelp();
				} else if ("1".equalsIgnoreCase(line.trim()))
				{
					selectListService();

				} else if ("2".equalsIgnoreCase(line.trim()))
				{
					saveOneServiceTest(br);
				} else if ("3".equalsIgnoreCase(line.trim()))
				{
					updateOneDynastyService(br);
				} else if ("4".equalsIgnoreCase(line.trim()))
				{
					deleteOneDynastyService(br);
				} else if ("5".equalsIgnoreCase(line.trim()))
				{
					selectOneService(br);
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void kingLaunch(BufferedReader br)
	{
		String line = "";
		try
		{
			while (true)
			{
				System.out.println("==皇上操作==按下n退出,按下h进入帮助菜单");
				line = br.readLine();
				if ("n".equalsIgnoreCase(line.trim()))
				{
					System.out.println("你退出了皇上操作,你已进入主菜单！");
					break;
				} else if ("h".equalsIgnoreCase(line.trim()))
				{
					this.kingHelp();
				} else if ("1".equalsIgnoreCase(line.trim()))
				{
					selectListKingService();
				} else if ("2".equalsIgnoreCase(line.trim()))
				{
					saveOneKingTest(br);
				} else if ("3".equalsIgnoreCase(line.trim()))
				{
					updateOneKingService(br);
				} else if ("4".equalsIgnoreCase(line.trim()))
				{
					deleteOneKingService(br);
				} else if ("5".equalsIgnoreCase(line.trim()))
				{
					selectOneKingService(br);
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/* 查询所有皇上 */
	public void selectListKingService()
	{
		System.out.println(dynastyServie.kingTitle());
		List<King> list = dynastyServie.selectListKingService();
		if (list != null)
		{
			for (King king : list)
			{
				System.out.println(king);
			}
		} else
		{
			System.out.println("这是一个空文件");
		}
	}

	/* 删除一条答案 */
	private void deleteOneAnswerService(BufferedReader br)
	{
		Answer an = this.selectOneAnswerService(br);
		if (an == null)
		{
			return;
		}
		this.qas.deleteOneAnswerService(an);
		System.out.println("删除成功，id==" + an.getId());
	}

	/* 查询单条答案 */
	private Answer selectOneAnswerService(BufferedReader br)
	{
		System.out.println("请输入要查询的ID");
		String id = null;
		try
		{
			id = br.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		id.trim();
		Map<String, Object> condMap = new TreeMap<String, Object>();
		try
		{
			condMap.put("id", Integer.valueOf(id));
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("输入的ID格式不正确");
		}
		Answer an = this.qas.selectOneAnswerService(condMap);
		System.out.println(an);
		return an;
	}

	/* 更新一条答案 */
	private void updateOneAnswerService(BufferedReader br)
	{
		Answer an = this.selectOneAnswerService(br);
		if (an == null)
		{
			return;
		}
		System.out.println("更新实例" + an.getStuId() + "|" + an.isShiFou());
		String line = null;
		try
		{
			line = br.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		line.trim();
		String[] lines = line.split("\\|");
		if (lines.length != 2)
		{
			System.out.println("输如的格式不正确");
			return;
		}
		an.setStuId(Integer.valueOf(lines[0]));
		if (lines[1] == "true")
		{
			an.setShiFou(true);
		} else
		{
			an.setShiFou(false);
		}
		this.qas.updateOneAnswerService(an);
		System.out.println("更新成功，id==" + an.getId());
	}

	/* 保存一条答案 */
	private void saveOneAnswerService(BufferedReader br)
	{
		System.out.println("请输入你的答案:答案|(1)正确或者(0)错误");
		System.out.println("示例:2|0");
		String line = null;
		try
		{
			line = br.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		line.trim();
		String[] lines = line.split("\\|");
		if (lines.length != 2)
		{
			System.out.println("输入格式不正确");
			return;
		}
		Answer an = new Answer();
		an.setJieGuo(lines[0]);
		an.setShiFou("1".equalsIgnoreCase(lines[1]) ? true : false);
		an.setCreateTime(new Date());
		an.setEndTime(new Date());
		this.qas.saveOneAnswerService(an);
		System.out.println("保存成功");
	}

	/* 查询所有答案 */
	private void selectListAnswerService()
	{
		List<Answer> an = this.qas.selectListAnswerService();
		if (an == null)
		{
			System.out.println("没有数据");
			return;
		}
		System.out.println(this.qas.printAnswerTiltle());
		for (Answer a : an)
		{
			System.out.println(a);
		}
	}

	/* 删除一个问题 */
	private void deleteOneQuestionService(BufferedReader br)
	{
		Question question = this.selectOneQuestionService(br);
		if (question == null)
		{
			return;
		}
		this.qas.deleteOneQuestionService(question);
		System.out.println("删除成功，id==" + question.getId());
	}

	/* 查询单个问题 */
	private Question selectOneQuestionService(BufferedReader br)
	{
		System.out.println("---请输入要查询的问题ID---");
		String id = null;
		Map<String, Object> condMap = new TreeMap<String, Object>();
		try
		{
			id = br.readLine();
			id.trim();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		try
		{
			condMap.put("id", Integer.valueOf(id));
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("输入的ID格式不正确");
		}
		Question question = this.qas.selectOneQuestionService(condMap);
		if (question != null)
		{
			System.out.println(this.qas.printQuestionTiltle());
			System.out.println(question);
		} else
		{
			System.out.println("无此纪录,查询失败");
		}
		return question;
	}

	/* 更新一个问题 */
	private void updateOneQuestionService(BufferedReader br)
	{
		Question question = this.selectOneQuestionService(br);
		if (question == null)
		{
			return;
		}
		System.out.println("更新实例" + question.getName() + "|" + question.getScore());
		String lines = null;
		try
		{
			lines = br.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		lines.trim();
		String[] line = lines.split("\\|");
		if (line.length != 2)
		{
			System.out.println("您输入的格式不正确");
			return;
		}
		question.setName(line[0]);
		question.setScore(Integer.valueOf(line[1]));
		this.qas.updateOneQuestionService(question);
		System.out.println("更新成功,id==" + question.getId());
	}

	/* 保存一个问题 */
	private void saveOneQuestionService(BufferedReader br)
	{
		System.out.println("请输入问题的相关信息:名字|20(分数)");
		System.out.println("示例:Date的用法|20");
		String line = "";
		try
		{
			line = br.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		String[] lines = line.split("\\|");
		if (lines.length != 2)
		{
			System.out.println("输入格式不正确");
			return;
		}
		Question qu = new Question();
		qu.setName(lines[0]);
		qu.setScore(Integer.valueOf(lines[1]));
		qu.setCreateTime(new Date());
		qu.setEndTime(new Date());
		this.qas.saveOneQuestionService(qu);
		System.out.println("保存成功,id:" + qu.getId());
	}

	/**
	 * 查询所有问题
	 */
	private void selectListQuestionService()
	{
		List<Question> question = this.qas.selectListQuestionService();
		if (question == null)
		{
			System.out.println("没有数据");
			return;
		}
		System.out.println(this.qas.printQuestionTiltle());
		for (Question qu : question)
		{
			System.out.println(qu);
		}
	}

	private void help()
	{
		System.out.println("1:对朝代进行操作");
		System.out.println("2:对皇上进行操作");
		System.out.println("3:对学生进行操作");
		System.out.println("4:对等级进行操作");
		System.out.println("5:查询所有问题");
		System.out.println("6:添加问题");
		System.out.println("7:修改问题;(先输入问题id,填写要修改的内容)");
		System.out.println("8:删除问题");
		System.out.println("9:查看单个问题");
		System.out.println("10:查询所有答案");
		System.out.println("11:添加答案");
		System.out.println("12:修改答案;(先输入答案id,填写要修改的内容)");
		System.out.println("13:删除答案");
		System.out.println("14:查看单个答案");
		System.out.println("50:开始抽奖");
		System.out.println("51:一键将文件导入数据库");
		System.out.println("52:一键将数据库导入文件");

	}

	private void dynastyHelp()
	{
		System.out.println("1:查询所有朝代");
		System.out.println("2:添加朝代");
		System.out.println("3:修改朝代;(先输入朝代id,填写要修改的内容)");
		System.out.println("4:删除朝代");
		System.out.println("5:查看单个朝代");
	}

	private void kingHelp()
	{
		System.out.println("1:查询所有皇上");
		System.out.println("2:添加皇上");
		System.out.println("3:修改皇上;(先输入皇上id,填写要修改的内容)");
		System.out.println("4:删除皇上");
		System.out.println("6:查询单个皇上");
	}

	public void setClient(UsersClient client)
	{
		this.client = client;
	}

	public void setUsersService(IUsersService usersService)
	{
		this.usersService = usersService;
	}

	public void setDynastyServie(IDynastyService dynastyServie)
	{
		this.dynastyServie = dynastyServie;
	}

	public void setQas(IQueAnsService qas)
	{
		this.qas = qas;
	}

}
